# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

pkgname=cmake
pkgver=3.12.1
pkgrel=1
pkgdesc="A cross-platform open-source make system"
arch=('i686' 'x86_64')
url="https://www.cmake.org/"
license=("MIT")
makedepends=("gcc" "pkg-config"
             jsoncpp-devel
             libcurl-devel
             libexpat-devel
             libarchive-devel
             librhash-devel
             libuv-devel
             ncurses-devel
             zlib-devel)
depends=("gcc-libs"
         "jsoncpp"
         "libcurl"
         "libexpat"
         "libarchive"
         "librhash"
         "libuv"
         "ncurses"
         "pkg-config"
         "zlib")
options=('staticlibs' 'strip')
source=("https://www.cmake.org/files/v3.12/${pkgname}-${pkgver}.tar.gz"
        "cmake-3.10.2-msys.patch"
        "disable-curses-for-msys.patch"
        "disable-response-files-for-msys.patch"
        "3.9.3-cpuinfo.patch"
        "3.9.3-cygwin-paths.patch")
sha256sums=('c53d5c2ce81d7a957ee83e3e635c8cda5dfe20c9d501a4828ee28e1615e57ab2'
            '209b2e53c675627146ecd1b018e824a34ce8e3d0e26175841e77f3b4f0a90f18'
            'b7e398e70b97088f88a0688e8a0794d6780d3cdb91075388a30908dc4b405eb7'
            '98dca846de0ca7b71884e26678317f85e78e01862d58a29ce923c835ca7d614f'
            '36c34df5d9647a043b59fbfc162a969547901adfb64cc0ca798d1af35c32a3a9'
            '80e241cacfc77e1ebd81b8ce721917e021d0dd72b8fb6773f057a5356bac1a4a')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${pkgname}-${pkgver}
  rm -f Modules/CPackMsys.cmake \
    Modules/FindMsys.cmake \
    Modules/Platform/MSYS-GNU.cmake \
    Modules/Platform/MSYS.cmake \
    Modules/Platform/MSYS-CXX.cmake \
    Modules/Platform/MSYS-GNU-C.cmake \
    Modules/Platform/MSYS-GNU-CXX.cmake \
    Modules/Platform/MSYS-GNU-Fortran.cmake \
    Modules/Platform/MSYS-windres.cmake || true
  apply_patch_with_msg "cmake-3.10.2-msys.patch" \
       "disable-response-files-for-msys.patch" \
       "3.9.3-cpuinfo.patch" \
       "3.9.3-cygwin-paths.patch"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  MSYSTEM=MSYS "${srcdir}"/${pkgname}-${pkgver}/configure \
    --prefix=/usr \
    --system-libs \
    --no-qt-gui \
    --parallel=$(nproc) \
    --mandir=share \
    --docdir=share/doc/cmake \
    -- -DCURSES_FORM_LIBRARY=/usr/lib/libformw.dll.a \
      -DCTEST_TEST_CTEST=ON \
      -DCMAKE_BUILD_TYPE=Release
  #Fix tests
  local  _gccver=$(gcc --version | grep ^gcc | sed 's/^.* //g')
  sed -e "s|4.9.2|${_gccver}|g" -e "s|4.8.2|${_gccver}|g" \
       -i ${srcdir}/build-${CARCH}/Tests/CMakeTests/ImplicitLinkInfoTest.cmake
  plain "Start building..."
  make
}

#check() {
#  cd "${srcdir}/build-${CARCH}"
#  make test
#}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR=$pkgdir install

  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/Copyright.txt \
    "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}
